const { app, BrowserWindow, ipcMain, MessageChannelMain } = require('electron')
const path = require('path')


const createWindow = () => {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: false,
      preload: path.join(__dirname, 'preload.js')
    }
  })

  const seceondaryWindow = new BrowserWindow({
    show: false,
    webPreferences: {
      contextIsolation: false,
      preload: "preloadSecondary.js"
    }
  })

  const { port1, port2 } = new MessageChannelMain()

  mainWindow.once("ready-to-show", () => {
    mainWindow.webContents.postMessage('port', null, [port1])
  })

  seceondaryWindow.once("ready-to-show", () => {
    seceondaryWindow.webContents.postMessage("port", null, [port2])
  })

  mainWindow.loadFile('index.html')

  // 打开开发工具
  mainWindow.webContents.openDevTools()
}

// 除了 macOS 外，当所有窗口都被关闭的时候退出程序。 因此, 通常
// 对应用程序和它们的菜单栏来说应该时刻保持激活状态,
// 直到用户使用 Cmd + Q 明确退出
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

app.whenReady().then(async () => {
  const worker = new BrowserWindow({
    show: false,
    webPreferences: {
      nodeIntegration: true
    }
  })
  await worker.loadFile("worker.html")
})
